package persistence.impl;

import domain.Order;
import persistence.DBUtil;
import persistence.OrderDAO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDAOImpl implements OrderDAO {

    private static final String GET_ORDERS_BY_USERID =
            "SELECT * FROM `order` WHERE user_id=?";
    private static final String GET_ORDER_BY_ORDERID =
            "SELECT * FROM `order` WHERE order_id=?";
    private static final String ADD_ORDER =
            "INSERT INTO `order` (unit_price,price,num,time,user_id,product_id,name,order_id,img_src,phone,address) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    private static final String DELETE_ORDER =
            "DELETE FROM `order` WHERE order_id = ";

    @Override
    public List<Order> getOrdersByUserid(String userid) {
        List<Order> orderList = new ArrayList<>();
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_ORDERS_BY_USERID);
            preparedStatement.setString(1, userid);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                orderList.add(toOrder(resultSet));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return orderList;
    }

    @Override
    public boolean addOrder(Order order) {
        boolean flag = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(ADD_ORDER);
            preparedStatement.setBigDecimal(1, order.getUnitprice());
            preparedStatement.setBigDecimal(2, order.getPrice());
            preparedStatement.setInt(3, order.getNum());
            preparedStatement.setTimestamp(4, order.getTime());
            preparedStatement.setString(5, order.getUserid());
            preparedStatement.setString(6, order.getProductid());
            preparedStatement.setString(7, order.getName());
            preparedStatement.setString(8, String.valueOf(System.currentTimeMillis()));
            preparedStatement.setString(9, order.getImageSrc());
            preparedStatement.setString(10, order.getPhone());
            preparedStatement.setString(11, order.getAddress());
            int row = preparedStatement.executeUpdate();
            if(row == 1){
                flag = true;
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean delOrder(String orderid) {
        boolean result = false;
        try {
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            int row = statement.executeUpdate(DELETE_ORDER + orderid);
            if(row == 1){
                result = true;
            }
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Order getOrderByOrderid(String orderid) {
        Order order = null;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_ORDER_BY_ORDERID);
            preparedStatement.setString(1, orderid);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                order = toOrder(resultSet);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return order;
    }

    private Order toOrder(ResultSet resultSet) {
        Order order = new Order();
        try {
            order.setUserid(resultSet.getString("user_id"));
            order.setUnitprice(resultSet.getBigDecimal("unit_price"));
            order.setPrice(resultSet.getBigDecimal("price"));
            order.setNum(resultSet.getInt("num"));
            order.setTime(resultSet.getTimestamp("time"));
            order.setProductid(resultSet.getString("product_id"));
            order.setName(resultSet.getString("name"));
            order.setImageSrc(resultSet.getString("img_src"));
            order.setOrderid(resultSet.getString("order_id"));
            order.setPhone(resultSet.getString("phone"));
            order.setAddress(resultSet.getString("address"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return order;
    }
}
